import argparse
import json
import json_tools
parser = argparse.ArgumentParser()

parser.add_argument('--startTime', type=int, default=0)
parser.add_argument('--endTime', type=int, required=True)
parser.add_argument('--oldfile', type=str, required=True)
parser.add_argument('--newfile', type=str, required=True)
parser.add_argument('--uniqueKey', type=str, default="timestamp")
parser.add_argument('--timeKey', type=str, default="timestamp")
args = parser.parse_args()

'''
1675248413229     1675248426157   
python3 diffkafkamsg_detail.py --startTime 1675248405000 --endTime 1675248435000 --oldfile "./v2x_3d_obstacle_data_old.log" --newfile "./v2x_3d_obstacle_data_new.log"
'''
def filterByUniqueKey(olditem, newitem):
    result1 = False
    uniqueKeys = args.uniqueKey.split(',')
    for uniqueKeyItem in uniqueKeys:
        if olditem[uniqueKeyItem] != newitem[uniqueKeyItem]:
            return result1
    result1 = True
    return result1

oldMsgList = []
with open(args.oldfile, "r") as f1:
    for line in f1.readlines():
        oldMsgTemp = json.loads(line)
        try:
            if oldMsgTemp[args.timeKey] >=  args.startTime and oldMsgTemp[args.timeKey] <=  args.endTime:
                oldMsgList.append(oldMsgTemp)
        except Exception as e:
            print('line:',line)
            print('oldMsg:',oldMsgTemp)
            print(e)

newMsgList = []
with open(args.newfile, "r") as f2:
    for line2 in f2.readlines():
        newMsgTemp = json.loads(line2)
        if newMsgTemp[args.timeKey] >=  args.startTime and newMsgTemp[args.timeKey] <=  args.endTime:
            newMsgList.append(newMsgTemp)

print("线上数据：%d条, 新链路数据：%d条"%(len(oldMsgList), len(newMsgList)))

# 1、判断多或者少数据
for oldMsgTemp in oldMsgList:
    oldTempFilterResult = list(filter(lambda x : filterByUniqueKey(x, oldMsgTemp), newMsgList))
    if len(oldTempFilterResult) > 0:
        # 2、判断同一条数据时，进行diff
        result = json_tools.diff(oldMsgTemp, oldTempFilterResult[0])
        #print("存在匹配个数:", len(oldTempFilterResult))
        #print("diff结果：", result)
    else:
        print("线上存在，但新链路不存在：", oldMsgTemp)

# 1、判断多或者少数据
for newMsgTemp in newMsgList:
    newTempFilterResult = list(filter(lambda x : filterByUniqueKey(x, newMsgTemp), oldMsgList))
    if len(newTempFilterResult) > 0:
        # 2、判断同一条数据时，进行diff，上面已经diff过了，这个直接past
        continue
        #result = json_tools.diff(newMsgTemp, newTempFilterResult[0])
        #print("存在匹配个数:", len(newTempFilterResult))
        #print("diff结果：", result)
    else:
        print("线上不存在，但新链路存在：", newMsgTemp)








